<?php
$T->loadTpl("./html/".$template."/".$qname.".html");
$T->SetTpl('cssjs','html/cssjs.inc'); 
$T->SetTpl('jsmodel','html/jsmodel.inc');     
  
$p=isset($_GET["p"])?$_GET["p"]:"1";
$offset = ($p-1)*15;
$uid=$_SESSION['uid'];
$t=isset($_GET["type"])?$_GET["type"]:"";
$tuid=isset($_GET["typeuid"])?$_GET["typeuid"]:"create";
$where = "";
if(!empty($t)){$where.=" and A.type=".$t;$T->Set('type',$t);}
if(!empty($tuid)){
    if($tuid=='create'){$where.=" and A.uid='".$uid."'";$T->Set('typeuid',$tuid);}
    else {$where.=" and locate('".$uid."',A.accepter)>0";$T->Set('typeuid',$tuid);}
} 
$sql = "select A.*,B.truename as create_name from sys_plan A left join sys_member B on A.uid=B.id  where 1=1".$where." order by A.time desc limit {$offset},15";     
$list =$T->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$str_html = '';
foreach ($list as $k => $val) {
    $dept_namearr = array();$accept_namearr = array();$charge_namearr = array();
    $type = $T->db->query("select name from sys_plan_type where id=".$val['type'])->fetchColumn(0);
    //发布部门
    $dept_idarr = explode(',', $val['dept_id']);
    foreach ($dept_idarr as $key => $deptid) {
       $dept_namearr[]=$T->db->query("select deptname from sys_department where id=".$deptid)->fetchColumn(0);
    }
    $dept_name=implode(',', $dept_namearr);
    //发布人员
    $accept_idarr = explode(',', $val['accepter']);
    foreach ($accept_idarr as $key => $acceptid) {
       $accept_namearr[]=$T->db->query("select truename from sys_member where id='".$acceptid."'")->fetchColumn(0);
    }
    $accept_name=implode(',', $accept_namearr);
    //负责人
    $charge_idarr = explode(',', $val['charge_uid']);
    foreach ($charge_idarr as $key => $chargeid) {
       $charge_namearr[]=$T->db->query("select truename from sys_member where id='".$chargeid."'")->fetchColumn(0);
    }
    $charge_name=implode(',', $charge_namearr);
    $now=date('Y-m-d',time());
    $status='';
    if($val['start_time']>$now){
        $status='未开始';
        $setid=1;$setname='立即开始';
    }elseif($val['start_time']<=$now){
        if(!$val['end_time']){
            $status='正在进行';
            $setid=2;$setname='立即结束';
        }elseif($val['end_time']<$now){
            $status='已结束';
            $setid=3;$setname='恢复计划';
        }else{
            $status='正在进行';
            $setid=2;$setname='立即结束';
        }
    }
    $str_html.='<tr>
                    <td>
                        <input type="checkbox" name="checkbox" lay-skin="primary" value="'.$val["id"].'"/>
                    </td>
                    <td class="ellipsis">'.$val["name"].'</td>
                    <td>'.$type.'</td>
                    <td>'.$val['start_time'].'</td>
                    <td>'.$val['end_time'].'</td>
                    <td class="ellipsis">'.$dept_name.'</td>
                    <td class="ellipsis">'.$charge_name.'</td>
                    <td>'.$val['time'].'</td>
                    <td>'.$status.'</td>
                    <td>    
                        <button class="layui-btn layui-btn-mini layui-btn-normal ajax"  data-opt="view"  data-id="'.$val["id"].'">
                            <i class="iconfont">&#xe605;</i>查看
                        </button>';
                    if($tuid=='create'){
                        $str_html.='<button class="layui-btn layui-btn-mini  ajax"  data-opt="edit"  data-id="'.$val["id"].'">
                            <i class="iconfont">&#xe605;</i>编辑
                        </button>
                        <button class="layui-btn layui-btn-mini layui-btn-danger ajax"  data-opt="del"  data-id="'.$val["id"].'">
                            <i class="iconfont">&#xe626;</i>删除
                        </button>
                        <button class="layui-btn layui-btn-mini layui-btn-warm ajax"  data-opt="set"  data-id="'.$val["id"].'" data-setid="'.$setid.'" data-setname="'.$setname.'">
                            <i class="iconfont">&#xe646;</i>'.$setname.'
                        </button>';
                    }
                    $str_html.='</td></tr>';
}
$rc=$T->db->query("select count(*) from sys_plan A where 1=1".$where)->fetchColumn(0);
if($p<1)$p=1;
$page=getPageHtml_bt($rc,15,$p,"&t=plan_sys");
$page=mb_convert_encoding($page, 'utf-8', 'utf-8');
$T->SetBlock("types","select id,name from sys_plan_type order by odx");
$T->Set("page",$page);
$T->Set("str_html",$str_html);
      
$T->Set("template",$template);  
$T->clearNaN();       
$T->clearNoN();
$T->display(); 